package cn.com.nes.common.util;

import cn.hutool.core.util.StrUtil;

public class HexUtils {
    public static String HexByte2Bit(String hexStr) {
        try {
            if (StrUtil.isBlank(hexStr)) {
                return "";
            }
            if (hexStr.length() % 2 != 0) {
                hexStr = "0" + hexStr;
            }
            byte[] bytes = new byte[hexStr.length() / 2];
            for (int i = 0; i < bytes.length; i++) {
                bytes[i] = (byte) Integer.parseInt(hexStr.substring(i * 2, i * 2 + 2), 16);
            }
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(Integer.toBinaryString((b & 0xFF) + 0x100).substring(1));
            }
            return sb.toString();
        } catch (Exception e) {
            return "";
        }
    }
}
